package cn.school.demo.ui.edit

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.MyApplication
import cn.school.demo.pojo.Result
import cn.school.demo.pojo.User
import cn.school.demo.repository.DataRepository

class EditInfoViewModel : ViewModel() {
    private lateinit var _result: LiveData<Result>
    val result: LiveData<Result>
        get() = _result
    private val _user = MutableLiveData<User>()
    val user: LiveData<User>
        get() = _user
    private val _isOnClick = MutableLiveData<Boolean>()
    val isOnClick: LiveData<Boolean>
        get() = _isOnClick
    private val _editTitle = MutableLiveData<String>()
    val editText: LiveData<String>
        get() = _editTitle

    private val _sex = MutableLiveData<Int>()
    val sex: LiveData<Int>
        get() = _sex
    private val _birthday = MutableLiveData<String>()
    val birthday: LiveData<String>
        get() = _birthday
    private val _area = MutableLiveData<String>()
    val area: LiveData<String>
        get() = _area
    private var _isModify = false
    val isModify: Boolean get() = _isModify

    init {
        _user.value = MyApplication.user
        _isOnClick.value = false
        _editTitle.value = "编辑"
        _area.value = MyApplication.user!!.area ?: ""
        _birthday.value = MyApplication.user!!.birthday ?: ""
        _sex.value = MyApplication.user!!.sex ?: 0
    }

    fun updateIsModify(b: Boolean) {
        _isModify = b
    }

    fun setPhone(phone: String) {
        _user.value!!.telephoneNumber = phone
    }

    fun setSex(sex: Int) {
        _user.value!!.sex = sex
        _sex.value = sex
        updateIsModify(true)
    }

    fun setBirthday(birthday: String) {
        _user.value!!.birthday = birthday
        _birthday.value = birthday
        updateIsModify(true)
    }

    fun setArea(area: String) {
        _user.value!!.area = area
        _area.value = area
        updateIsModify(true)
    }

    fun setIsOnClick() {
        _isOnClick.value = !_isOnClick.value!!
    }

    fun setTitle(t: String) {
        _editTitle.value = t
    }

    fun editUserInfo() {
        _result = DataRepository().editUserInfo(_user.value!!)
    }

    fun saveHeadImage(): LiveData<Result> =
        DataRepository().formUpdate(MyApplication.user!!.sfNumber)

}